package com.app.Activity;

import java.io.File;
import java.util.ArrayList;

import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.DTO.AccountDTO;
import com.app.DTO.RoleDTO;
import com.app.PhanQuyen.Quyen;

import ReadWriteXML.CMember;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//--

public class frmTaoNhom extends Activity {
	public static String CODE_GROUP_NAME="CODE_GROUP_NAME";
	int port=5033;
	Button btnTaoNhom;
	Button btnCaiDatVaiTro;
	EditText txtTenNhom;
	EditText txtSoLuongThanhVien;
	@Override
	public void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_tao_nhom);
		btnTaoNhom=(Button)findViewById(R.id.btnTaoNhom);
		btnCaiDatVaiTro=(Button)findViewById(R.id.btnCaiDatVaiTro);
		txtTenNhom=(EditText)findViewById(R.id.txtTenNhom);
		txtSoLuongThanhVien=(EditText)findViewById(R.id.txtSoLuongThanhVien);
		
		btnTaoNhom.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {			
				//Tạo file danh sách thành viên				
				createMemberFile();				
				NetworkController._peer.account.isThanhLapNhom = true;
				Intent i = new Intent(getBaseContext(), frmNhomChoActivity.class);
				i.putExtra(CODE_GROUP_NAME, txtTenNhom.getText().toString());
				NetworkController.setGroupName(String.valueOf(txtTenNhom.getText()));
				
				//ghi xuống file Log
//				String text = "Đã tạo nhóm.." + txtTenNhom.getText();
//				ReadWriteFileController.checkRuleWriteToLogFile(text);
				
				startActivity(i);
			}
		});
		
		btnCaiDatVaiTro.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {		

				Intent i=new Intent(getBaseContext(),frmQuanLyRoleActivity.class);
				
				//ghi xuống file Log
//				String text = "Đã vào Quản lý Role..";
//				ReadWriteFileController.checkRuleWriteToLogFile(text);
		        
				startActivity(i);
			}
		});
	}
	public static void createMemberFile(){
		CMember member=new CMember();
		member.set_fullName(NetworkController._peer.account.get_Username());
		member.set_ip(NetworkController._peer.getAddressPeer());
		member.set_userName(NetworkController._peer.account.get_Username());		
		ReadWriteFileController.deleteFile(ReadWriteFileController.MEMBER_PATH);		
		ReadWriteFileController.insertMember(ReadWriteFileController.MEMBER_PATH, member);
	}
	@Override
	public void onStart(){
		super.onStart();
		if (NetworkController._peer == null) {
			AccountDTO account = new AccountDTO();
			NetworkController.init("MES", port, account);	
		}
		ArrayList<Quyen> ruleList=ReadWriteFileController.readRule(getApplicationContext(), ReadWriteFileController.RULE_BASE_PATH);
		RoleDTO roleSangLapNhom=new RoleDTO("Người sáng lập nhóm", ruleList);		
		NetworkController._peer.getAccount().addRole(roleSangLapNhom);
		
	}
	
	//An
	/*public void WriteToLogFile(String text){
		Log.d("Phu", NetworkController._peer.getAccount().get_Username() + "-"+ text );
        String username = NetworkController._peer.getAccount().get_Username();
		Log.d("Phu", "ten nhom 2....: "+ txtTenNhom.getText());
		String path = ReadWriteFileController.DIRECTORY + txtTenNhom.getText()+"LogList.xml";
		Log.d("Phu", "Path: "+ path);
        ReadWriteFileController.insertLogFile(path, text, username);
	}*/

}
